import { defineStore } from "pinia";
import { TANK } from '@/utils/global'

export const useEnemyStore = defineStore('enemy', {
  state: () => ({
    enemies: new Map(),
    id: 0,
  }),
  actions: {
    bornEnemy({ type, shape, color, position }) {
      const o = {
        id: `enemy${this.id}`,
        type, // basic fast power armor
        shape, // 1-8
        color,
        x: TANK.position[position].x,
        y: TANK.position[position].y,
        direction: TANK.direction,
        moving: false, // 是否处于移动状态
        canMove: true, // 是否可以移动
        ...TANK[type],
        hp: color === 'silver' ? TANK[type].hp : TANK[type].hp + 1, // 黄、绿、红坦克被击中后，会先变成对应的银色坦克类型，然后变成银色basic坦克，最后消失，所以hp会多1
        canFire: true, // 是否可以开火
      }
      this.enemies.set(o.id, o)
      this.id++
    },
    updateEnemy({ id, ...params }) {
      const item = this.enemies.get(id)
      this.enemies.set(id, { ...item, ...params })
    },
    enemyDestroyed(id) {
      this.enemies.delete(id)
    },
    clearEnemy() {
      this.enemies.clear()
    }
  }
})